package com.qianfeng.lingshijia;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;

import adapter.SchoolSelectAdapter;
import entity.SchoolListEntity;
import entity.SchoolSelectEntity;
import utils.Constants;
import utils.DownloadUtils;

/**
 * 选择大学的页面
 */
@ContentView(R.layout.activity_school_select)
public class SchoolSelectActivity extends AppCompatActivity {

    @ViewInject(R.id.lvSchoolSelect)
    private ListView lvSchoolSelect;

    @OnClick(R.id.rlBackToSMActivity)
    public void OnClick(View v){
        finish();
    }

    List<SchoolListEntity> schoolList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);

        //页面请求大学的Json数据
        DownloadUtils.getJson(Constants.URL.SCHOOL_SELECT_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {//成功回调方法
                //获取Json通过Gson转成对象
                Gson gson = new Gson();
                //获取校区和大学的数组
                SchoolSelectEntity ssEntity = gson.fromJson(responseInfo.result, SchoolSelectEntity.class);
                List<SchoolSelectEntity.DataEntity.AuListEntity> list = ssEntity.getData().getAuList();
                //遍历数组取出数据统一放在ScchoolListEntity的List里面
                for (int i = 0; i < list.size(); i++) {
                    SchoolSelectEntity.DataEntity.AuListEntity auListEntity = list.get(i);
                    SchoolListEntity slEntity = new SchoolListEntity();
                    SchoolSelectEntity.DataEntity.AuListEntity.AreaEntity areaEntity = auListEntity.getArea();
                    slEntity.setId(areaEntity.getId());
                    slEntity.setCode(areaEntity.getCode());
                    slEntity.setName(areaEntity.getName());
                    schoolList.add(slEntity);

                    List<SchoolSelectEntity.DataEntity.AuListEntity.UniversityListEntity> ulList = auListEntity.getUniversityList();
                    for (int j =0;j<ulList.size();j++){
                        SchoolSelectEntity.DataEntity.AuListEntity.UniversityListEntity uEntity = ulList.get(j);
                        SchoolListEntity slEntity2 = new SchoolListEntity();
                        slEntity2.setId(uEntity.getId());
                        slEntity2.setCode(uEntity.getCode());
                        slEntity2.setName(uEntity.getName());
                        slEntity.setType(1);
                        schoolList.add(slEntity2);
                    }
                }
                //为ListView设置适配器
                lvSchoolSelect.setAdapter(new SchoolSelectAdapter(schoolList,SchoolSelectActivity.this));
            }

            @Override
            public void onFailure(HttpException error, String msg) {//失败回调方法
                Toast.makeText(SchoolSelectActivity.this,"数据下载失败",Toast.LENGTH_LONG).show();
            }
        });
    }
}